<?php
namespace App\Http\Controllers\Admin;

use App\Models\Group;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Input;
use App\Models\User;
use App\Models\SystemSetting;


class GroupController extends BaseController
{
    protected $catgory;

    /**
     * @see \App\Http\Controllers\Admin\BaseController::__construct()
     */
    public function __construct() {
        parent::__construct();
        $this->catgory = Group::where(array('parent_id'=>0))->get();
        $this->middleware('init');
    }
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index(Request $request)
    {
        $group = Group::where(['parent_id'=>0])->orderBy('sort')->orderBy('created_at')->get();

        return view('admin.refuel.category.index')
            ->with('category', $group);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create(Request $req)
    {
       $cates =Group::whereIn('class_id',[1,2])->get();
        $fid = $req->input('catid','-1');
        
        $fgro = Group::find($fid);
        

        return view("admin.refuel.category.add")
            ->with('fgro', $fgro)
            ->with('type',$this->catgory)->with('cates',$cates)->with('blademode','add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        $mode = $request->input('blademode','add');
        $name = $request->input('cat_name','');
        if(empty($name)){
            abort(404);
        }
        if($mode== 'edit')
        {
            $id = $request->input('id',0);
            if($id <= 0){
                abort(404);
            }
            $category      = Group::find($id);
            $category->name      = $name;
            $category->save();
        }
        elseif($mode == 'add')
        {
            $parent_id = $request->input('type',0);
            if($parent_id >0){
                $parent = Group::find($parent_id);
                if(empty($parent)){
                    abort(404);
                }
                $class_id = $parent->class_id + 1;
            }else{
                $class_id = 1;
            }

            $insert              = [
                'name'               => $name,
                'parent_id'          => $parent_id,
                'class_id'          => $class_id,
            ];
            Group::firstOrCreate($insert);
        }else{
            abort('404');
        }

        return redirect("admin/group");
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id,Request $req)
    {
        $group = Group::find($id);
        $class_id = $group->class_id - 1;
        $pgroup = Group::where('class_id',$class_id)->get();
        return view("admin.refuel.category.add")
            ->with('group',$group)
            ->with('id',$id)
            ->with('pgroup',$pgroup)
            ->with('blademode','edit');
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        $folder = Group::find($id);
        $user = User::where('group_id',$id)->get();
        if(count($user) > 0){
            return  Response::json(array('status'=>'error','msg'=>'该组下面用户，请先调整用户的分组'));
        }
        $childs = Group::where('parent_id',$id)->get();
        if(count($childs) > 0){
            return  Response::json(array('status'=>'error','msg'=>'该组下面有子分组，请先删除子分组'));
        }

        if ($folder->delete())
            return  Response::json(array('status'=>'success','msg'=>'分组删除成功!'));
        else
            return  Response::json(array('status'=>'error','msg'=>'删除失败'));
    }

}


